import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
import 'package:lai_zhan/common/redux/AppState.dart';
import 'package:lai_zhan/common/style/AppStyle.dart';
import 'package:lai_zhan/common/util/NavigatorUtil.dart';
import 'package:lai_zhan/widget/LzTextFormField.dart';
import 'package:lai_zhan/widget/FlexButton.dart';
import 'package:lai_zhan/widget/CompactButton.dart';
import 'package:lai_zhan/common/dao/UserDao.dart';
import 'package:lai_zhan/common/util/RegularUtil.dart';
import 'package:lai_zhan/widget/BaseScaffold.dart';
import 'package:lai_zhan/common/dao/DataResult.dart';
class LoginPage extends StatefulWidget {
  static String rName = "login";

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _LoginPageState();
  }
}

class _LoginPageState extends State<LoginPage> {
  String _account;
  String _pwd;
  String _errorAccount;
  String _errorPwd;
  TextEditingController accountController = new TextEditingController();
  TextEditingController pwdController = new TextEditingController();
  GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
  FocusNode _accountFocusNode = new FocusNode();
  FocusNode _pwdFocusNode = new FocusNode();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _accountFocusNode.addListener(_accountListener);
    _pwdFocusNode.addListener(_pwdListener);
  }

  _accountListener() async {
    if(!_accountFocusNode.hasFocus) {
      setState(() {
        this._errorAccount = null;
      });
    }
  }

  _pwdListener() async {
    if(!_pwdFocusNode.hasFocus) {
      setState(() {
        this._errorPwd = null;
      });
    }
  }

  @override
  Widget build(BuildContext context) {

    Store<AppState> store = StoreProvider.of(context);
    // TODO: implement build
    return new StoreBuilder<AppState>(builder: (context, state) {
      return new GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          FocusScope.of(context).requestFocus(new FocusNode());
        },
        child: BaseScaffold(
          title: '登陆',
          iconColor: Colors.black,
          back: (){
            NavigatorUtil.goBack(context);
          },
          body: new Center(
            child: new Container(
              height: 380,
              margin: EdgeInsets.only(left: 30,right: 30),
              child: new Card(
                elevation: 5,
                child: new Container(
                  padding: EdgeInsets.only(left: 20,right: 20),
                    child: new Theme(
                      data: ThemeData(primaryColor: Color(AppColors.primaryColor)),
                      child: new Form(
                        key: _formKey,
                          child: new ListView(
                            children: <Widget>[
                              new Padding(padding: EdgeInsets.only(top: 50)),
                              new LzTextFormField(
                                controller: accountController,
                                labelText: '输入手机号',
                                icon: new Icon(Icons.phone_android),
                                keyboardType: TextInputType.numberWithOptions(),
                                errorText: _errorAccount,
                                onSave: (val) => _account = val,
                                validator: (val) {
                                  if (val == null || val.trim().isEmpty) {
                                    return "手机号不能为空";
                                  } else if (!RegularUtil.isChinaPhoneLegal(val.trim())) {
                                    return "请输入正确的手机号";
                                  } else {
                                    return null;
                                  }
                                },
                              ),
                              new LzTextFormField(
                                controller: pwdController,
                                labelText: '请输入密码',
                                icon: new Icon(Icons.vpn_key),
                                errorText: _errorPwd,
                                obscureText: true,
                                onSave: (val) => _pwd = val,
                                validator: (val) => (val==null||val.trim().isEmpty)?'密码不能为空':null,
                              ),
                              new Padding(padding: EdgeInsets.only(top: 20)),
                              new FlexButton(
                                textColor: Color(AppColors.white),
                                text: '登陆',
                                color: Color(AppColors.primaryColor),
                                onPress: (){
                                  var _form = _formKey.currentState;
                                  UserDao.login("", "", store).then((re){
                                    print(re.data);
                                    if(re.result){
                                      NavigatorUtil.goBack(context);
                                      NavigatorUtil.goIndex(context);
                                    }
                                  });
//                                  if(_form.validate()) {
//                                    _form.save();
//                                    print(_account);
//                                    print(_pwd);
//
//                                  }

                                  NavigatorUtil.goBack(context);
                                  NavigatorUtil.goIndex(context);
                                },
                              ),
                              new Padding(padding: EdgeInsets.only(top: 10)),
                              new Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  CompactButton(text:'忘记密码',textColor: Color(AppColors.cyan),onPress: (){

                                  },),
                                  new Text('|没有账号赶紧',style: TextStyle(fontSize: 12),),
                                  CompactButton(text:'注册一个',textColor: Color(AppColors.cyan),onPress: (){
                                    NavigatorUtil.goRegister(context);
                                  },)
                                ],
                              ),
                              new Padding(padding: EdgeInsets.only(top: 30))
                            ],
                          )),
                    )),
              ),
            ),
          ),
        ),
      );
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _accountFocusNode.removeListener(_accountListener);
    _pwdFocusNode.removeListener(_pwdListener);
    super.dispose();
  }
}
